<?php

namespace app\index\service;

use app\index\model\UM as UM;
use ShidianUtil\Exception\DataNotExistException;
use app\index\dao\ClientCacheDao;
use app\common\model\ClientCache;
use think\Collection;
use think\exception\ValidateException;

/**
 * Class CacheService
 * @package app\index\service
 */
class CacheService
{
    // dao
    private ClientCacheDao $clientCacheDao;

    public function __construct(ClientCacheDao $clientCacheDao)
    {
        $this->clientCacheDao = $clientCacheDao;
    }

    /**
     * 查询
     * @param UM|null $user
     * @param int $id
     * @return ClientCache
     */
    public function read(?UM $user, int $id): ?ClientCache
    {
        //这里如果要修改查询，建议把查询放到dao层去
        return $this->clientCacheDao->findByPK($id, true);
    }

    /**
     * 新增
     * @param UM $user
     * @param array $params
     * @return  ClientCache
     */
    public function add(?UM $user, array $params): ClientCache
    {
        $data = $params['data'] ?? null;
        if(!is_string($data)){
            throw new ValidateException('请输入内容');
        }
        if(is_blank($data)){
            throw new ValidateException('缓存内容不能为空');
        }

        return $this->clientCacheDao->insertActive($params, []);
    }

}
